package com.qianhui.dao;

import com.qianhui.common.IResult;
import com.qianhui.pojo.OrderDeclaration;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * OrderDeclaration entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see com.qianhui.pojo.OrderDeclaration
 */
public class OrderDeclarationDAO extends BaseHibernateDAO
{
    private static final Logger log = LoggerFactory.getLogger(OrderDeclarationDAO.class);
    // property constants
    public static final String ORDER_ID = "orderId";
    public static final String TENPAY_STATUS = "tenpayStatus";
    public static final String TENPAY_CODE = "tenpayCode";
    public static final String TENPAY_INFO = "tenpayInfo";
    public static final String HAIGUAN_STATUS = "haiguanStatus";
    public static final String HAIGUAN_CODE = "haiguanCode";
    public static final String HAIGUAN_INFO = "haiguanInfo";
    public static final String ZHIJIAN_STATUS = "zhijianStatus";
    public static final String ZHIJIAN_CODE = "zhijianCode";
    public static final String ZHIJIAN_INFO = "zhijianInfo";
    public static final String ZHUOZHI_STATUS = "zhuozhiStatus";
    public static final String ZHUOZHI_CODE = "zhuozhiCode";
    public static final String ZHUOZHI_INFO = "zhuozhiInfo";

    public OrderDeclaration save(OrderDeclaration transientInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return transientInstance;
    }

    public List<OrderDeclaration> saveList(List<OrderDeclaration> list)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            for (OrderDeclaration temp : list)
            {
                getSession().saveOrUpdate(temp);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return list;
    }

    public int delete(OrderDeclaration persistentInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public OrderDeclaration findById(java.lang.String id)
    {
        log.debug("getting OrderDeclaration instance with id: " + id);
        try
        {
            OrderDeclaration instance = (OrderDeclaration) getSession().get("com.qianhui.pojo.OrderDeclaration", id);
            return instance;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding OrderDeclaration instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from OrderDeclaration as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByTenpayStatus(Object tenpayStatus)
    {
        return findByProperty(TENPAY_STATUS, tenpayStatus);
    }

    public List findByTenpayCode(Object tenpayCode)
    {
        return findByProperty(TENPAY_CODE, tenpayCode);
    }

    public List findByTenpayInfo(Object tenpayInfo)
    {
        return findByProperty(TENPAY_INFO, tenpayInfo);
    }

    public List findByHaiguanStatus(Object haiguanStatus)
    {
        return findByProperty(HAIGUAN_STATUS, haiguanStatus);
    }

    public List findByHaiguanCode(Object haiguanCode)
    {
        return findByProperty(HAIGUAN_CODE, haiguanCode);
    }

    public List findByHaiguanInfo(Object haiguanInfo)
    {
        return findByProperty(HAIGUAN_INFO, haiguanInfo);
    }

    public List findByZhijianStatus(Object zhijianStatus)
    {
        return findByProperty(ZHIJIAN_STATUS, zhijianStatus);
    }

    public List findByZhijianCode(Object zhijianCode)
    {
        return findByProperty(ZHIJIAN_CODE, zhijianCode);
    }

    public List findByZhijianInfo(Object zhijianInfo)
    {
        return findByProperty(ZHIJIAN_INFO, zhijianInfo);
    }

    public List findByZhuozhiStatus(Object zhuozhiStatus)
    {
        return findByProperty(ZHUOZHI_STATUS, zhuozhiStatus);
    }

    public List findByZhuozhiCode(Object zhuozhiCode)
    {
        return findByProperty(ZHUOZHI_CODE, zhuozhiCode);
    }

    public List findByZhuozhiInfo(Object zhuozhiInfo)
    {
        return findByProperty(ZHUOZHI_INFO, zhuozhiInfo);
    }

    public OrderDeclaration findByOrderId(String orderId)
    {
        try
        {
            String sql = "from OrderDeclaration o where o.orderId=:orderId";
            Query query = getSession().createQuery(sql);
            query.setParameter("orderId", orderId);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return (OrderDeclaration) tempList.get(0);
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByTenpayStatusAndIndexAndPageSizeAndStatus(int index, int pageSize, String status)
    {
        try
        {
            String sql = "from OrderDeclaration o where o.tenpayStatus=:status order by o.tenpayStartTime asc,o.haiguanStartTime asc,o.zhijianStartTime asc,o.zhuozhiStartTime asc";
            Query query = getSession().createQuery(sql);
            query.setParameter("status", status);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByTenpayStatus(String status)
    {
        try
        {
            String sql = "select count(*) from OrderDeclaration o where o.tenpayStatus=:status";
            Query query = getSession().createQuery(sql);
            query.setParameter("status", status);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByHaiguanStatusAndIndexAndPageSizeAndStatus(int index, int pageSize, String status)
    {
        try
        {
            String sql = "from OrderDeclaration o where o.haiguanStatus=:status order by o.tenpayStartTime asc,o.haiguanStartTime asc,o.zhijianStartTime asc,o.zhuozhiStartTime asc";
            Query query = getSession().createQuery(sql);
            query.setParameter("status", status);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByHaiguanStatus(String status)
    {
        try
        {
            String sql = "select count(*) from OrderDeclaration o where o.haiguanStatus=:status";
            Query query = getSession().createQuery(sql);
            query.setParameter("status", status);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByZhijianStatusAndIndexAndPageSizeAndStatus(int index, int pageSize, String status)
    {
        try
        {
            String sql = "from OrderDeclaration o where o.zhijianStatus=:status order by o.tenpayStartTime asc,o.haiguanStartTime asc,o.zhijianStartTime asc,o.zhuozhiStartTime asc";
            Query query = getSession().createQuery(sql);
            query.setParameter("status", status);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByZhijianStatus(String status)
    {
        try
        {
            String sql = "select count(*) from OrderDeclaration o where o.zhijianStatus=:status";
            Query query = getSession().createQuery(sql);
            query.setParameter("status", status);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByZhuozhiStatusAndIndexAndPageSizeAndStatus(int index, int pageSize, String status)
    {
        try
        {
            String sql = "from OrderDeclaration o where o.zhuozhiStatus=:status order by o.tenpayStartTime asc,o.haiguanStartTime asc,o.zhijianStartTime asc,o.zhuozhiStartTime asc";
            Query query = getSession().createQuery(sql);
            query.setParameter("status", status);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByZhuozhiStatus(String status)
    {
        try
        {
            String sql = "select count(*) from OrderDeclaration o where o.zhuozhiStatus=:status";
            Query query = getSession().createQuery(sql);
            query.setParameter("status", status);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public OrderDeclaration findByZhijianMessageId(String zhijianMessageId)
    {
        try
        {
            String sql = "from OrderDeclaration od where od.zhijianMessageId=:zhijianMessageId";
            Query query = getSession().createQuery(sql);
            query.setParameter("zhijianMessageId", zhijianMessageId);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return (OrderDeclaration) tempList.get(0);
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }


}